package com.oneday.nuo.base.service.impl;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.oneday.nuo.base.config.BaseService;
import com.oneday.nuo.base.dao.NuoRoleDAO;
import com.oneday.nuo.base.dto.NuoRoleDTO;
import com.oneday.nuo.base.entity.NuoRoleEntity;
import com.oneday.nuo.base.entity.NuoUserEntity;
import com.oneday.nuo.base.exception.NuoException;
import com.oneday.nuo.base.response.NuoErrorStatus;
import com.oneday.nuo.base.service.NuoRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 角色表(NuoRole)表服务实现类
 *
 * @author liushuai
 * @since 2021-03-13 23:37:28
 */
@Slf4j
@Service("nuoRoleService")
public class NuoRoleServiceImpl extends BaseService<NuoRoleDAO, NuoRoleEntity> implements NuoRoleService {

    @Autowired
    private NuoRoleDAO nuoRoleDao;


    @Override
    public void saveRole(NuoRoleDTO nuoRoleDTO) {
        if(queryUserFieldIsExist(NuoRoleEntity::getRoleCode,nuoRoleDTO.getRoleCode(),nuoRoleDTO.getId())){
            throw  new NuoException("角色编号已存在");
        }
        NuoRoleEntity dto = Convert.convert(NuoRoleEntity.class,nuoRoleDTO);
        this.saveOrUpdate(dto);

    }

    @Override
    public void deleteById(String id) {
        nuoRoleDao.deleteById(id);
    }

    @Override
    public NuoRoleEntity queryRoleById(String id) {
        return nuoRoleDao.selectById(id);
    }

    @Override
    public List<NuoRoleEntity> queryRolePageByParam(Map<String, Object> param) {
        String roleName = (String) param.get("roleName");
        LambdaQueryWrapper<NuoRoleEntity> wrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(roleName)){
            wrapper.like(NuoRoleEntity::getRoleName,roleName);
        }
        return nuoRoleDao.selectList(wrapper);
    }

    @Override
    public List<NuoRoleEntity> queryRoleAll() {
        return nuoRoleDao.selectList(new QueryWrapper<>());
    }


    public Boolean queryUserFieldIsExist(SFunction<NuoRoleEntity, ?> field, String fieldValue, Object id) {
        if(StrUtil.isEmpty(fieldValue)){
            log.error("参数不能为空->{}",fieldValue);
            throw  new NuoException(NuoErrorStatus.PARAM_IS_NOT_NULL);
        }
        LambdaQueryWrapper<NuoRoleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(field,fieldValue);
        NuoRoleEntity nuoRoleEntity = nuoRoleDao.selectOne(wrapper);
        if(ObjectUtil.isNotNull(nuoRoleEntity)){
            if(nuoRoleEntity.getId().equals(id)){
                //查出的数据与id相等，则为修改
                return false;
            }
            //若不相等则查出的数据并不是修改内容，则为存在
        }else{
            //通过用户名未查出来数据，则不存在
            return  false;
        }
        return true;
    }
}